<?php

namespace app\exam\service\admin;

use app\common\service\BaseService;
use app\exam\logic\admin\QuestionDifficult as QuestionDifficultLogic;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

class QuestionDifficult extends BaseService
{

    /**
     * @Inject()
     *
     * @var QuestionDifficultLogic
     */
    protected $questionDifficultLogic;

    /**
     * 列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/3
     *
     * @return mixed
     */
    public function index()
    {
        $data = $this->questionDifficultLogic->getList([]);

        // 定义字段
        $keyList = [
            'title'       => ListItem::make()->title('难度名称'),
            'update_time' => ListItem::make()->title('最后更新时间'),
        ];
        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 创建试题难度
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/3
     *
     * @return \think\Response
     */
    public function create()
    {
        $data = Request::post();
        $res  = $this->questionDifficultLogic->createDifficult($data);

        return $this->parseToData($res->toArray(), 1, '添加成功');


    }

    /**
     * 编辑试题难度
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/4
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit(int $id)
    {
        $data = Request::put();
        $res  = $this->questionDifficultLogic->editDifficult($id, $data);
        return $this->parseToData($res->toArray(), 1, '编辑成功');

    }

    /**
     * 删除试题难度
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/4
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete(int $id)
    {
        $this->questionDifficultLogic->deleteDifficult($id);

        return $this->parseToData([], 1, '删除成功');
    }

}